package com.hzit.hzb.sms.control.api;

import com.hzit.common.exception.Assert;
import com.hzit.common.result.R;
import com.hzit.common.result.ResponseEnum;
import com.hzit.common.util.RegexValidateUtils;
import com.hzit.hzb.sms.client.UserInfoClient;
import com.hzit.hzb.sms.service.SmsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@RestController
@RequestMapping("/api/sms")
@Api(tags = "短信管理")
@Slf4j
public class ApiSmsController {
	@Autowired
	private SmsService smsService;
	@Autowired
	private UserInfoClient userInfoClient;

	//1. 发送手机验证码
	@ApiOperation("获取验证码")
	@GetMapping("/send/{mobile}")
	public R send(@PathVariable("mobile") String mobile) throws Exception {
		R r = userInfoClient.checkMobile(mobile);
		Assert.isTrue(r.getCode() != -1, ResponseEnum.ERROR);
		boolean isExist = (boolean) r.getData().get("isExist");
		Assert.isTrue(!isExist,ResponseEnum.MOBILE_EXIST_ERROR);
		// 1.1 验证手机号是否为空
		Assert.notNull(mobile, ResponseEnum.MOBILE_NULL_ERROR);
		// 1.2 使用工具类验证手机号
		Assert.isTrue(RegexValidateUtils.checkCellphone(mobile),ResponseEnum.MOBILE_ERROR);
		// 1.3 发送手机验证码
		smsService.send(mobile);

		return R.ok().message("发送验证码成功");
	}
}
